package com.uphone.magicleafteacher.base;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.radish.framelibrary.indicator.BaseIndicatorAdapter;
import com.radish.framelibrary.indicator.IndicatorAdapter;
import com.radish.framelibrary.indicator.TrackIndicatorView;
import com.uphone.magicleafteacher.R;

import butterknife.BindView;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/19
 *     desc   :
 * </pre>
 */
public abstract class BaseTabActivity extends BaseGActivity {
    @BindView(R.id.track_indicator)
    protected TrackIndicatorView trackIndicator;
    @BindView(R.id.viewPager)
    protected ViewPager viewPager;
    @BindView(R.id.ll)
    protected LinearLayout ll;
    @BindView(R.id.ll_tab)
    protected LinearLayout llTab;
    private Fragment[] fragmentList;
    protected boolean isLoad = true;
    protected int currentPosition = 0;
    protected int bottomTrackWidth = SizeUtils.dp2px(20);


    @Override
    protected int initLayout() {
        return R.layout.activity_base_tab;
    }

    @Override
    public void initData() {
        if (isLoad) {
            trackIndicator.setBottomTrackWidth(bottomTrackWidth);
            fragmentList = getFragmentList();
            initViewPager();
            initIndicator();
            viewPager.setCurrentItem(currentPosition);
        }
    }

    protected abstract Fragment[] getFragmentList();

    private void initViewPager() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList[i];
            }

            @Override
            public int getCount() {
                return fragmentList.length;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            }
        });
    }

    /**
     * 添加指示器
     */
    private void initIndicator() {
        BaseIndicatorAdapter adapter = initAdapter();
        trackIndicator.setAdapter(viewPager, adapter);
    }

    protected BaseIndicatorAdapter initAdapter() {
        return new IndicatorAdapter(this, getItems());
    }

    protected abstract String[] getItems();

}
